package thu.sorit.service;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.List;

import thu.sorit.Main;
import thu.sorit.resolver.SinaResolver;
import thu.sorit.resolver.Util;
import thu.sorit.resolver.Resolver;
import thu.sorit.resolver.Video;
import thu.sorit.resolver.TudouResolver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class SoritService extends Service {

	public class SoritBinder extends Binder {
		
		public SoritService getService() {
			return SoritService.this;
		}
		
	}
	
	private SoritBinder binder = new SoritBinder();
	
	private WifiListener wifiListener = new WifiListener(this);
	
	private ScreenListener screenListener = new ScreenListener(this);
	
	private NotificationManager notificationManager;
	
	private boolean isWifiOn = false;
	
	private boolean isScreenOff = false;
	
	private TestThread thread = null;
	
	private List<Video> videoList = new ArrayList<Video>();
	
	private Resolver resolver[] = new Resolver[] {
		new TudouResolver(),
		new SinaResolver()
	};
	
	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}
	
	public Resolver[] getResolverList() {
		return resolver;
	}
	
	public List<Video> getVideoList() {
		return videoList;
	}
	
	@Override
    public void onCreate() {
		setNotification();
        registerReceiver(wifiListener, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        IntentFilter screenFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        screenFilter.addAction(Intent.ACTION_SCREEN_ON);
        registerReceiver(screenListener, screenFilter);
    }
	
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    	return START_STICKY;
    }
    
    public void startSort() {
    	checkStart();
    }
    
    private void checkStart() {
    	if (isWifiOn && isScreenOff) {
    		if (thread == null || !thread.getEnabled()) {
    			thread = new TestThread(this, "http://www.tudou.com/programs/view/7EdK-n-gdiw/");
    			thread.start();
    		}
    	}
    }
    
    private void setNotification() {
        // Get the NotificationManager reference.
        String ns = Context.NOTIFICATION_SERVICE;
        notificationManager = (NotificationManager) getSystemService(ns);
        // Build our Notification.
        int icon = thu.sorit.R.drawable.ic_launcher;
        String ticker = "Sorit Running...";
        long now = System.currentTimeMillis();
        Notification notification = new Notification(icon, ticker, now);
        // Define the Notification's message and PendingIntent.
        String title = "Sorit";
        String description = "Sort your videos";
        Intent intent = new Intent(this, Main.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification.setLatestEventInfo(this, title, description, pendingIntent);
        notificationManager.notify(1, notification);
        startForeground(1, notification);
    }
    	
	public void onDestroy() {
		this.unregisterReceiver(wifiListener);
		this.unregisterReceiver(screenListener);
		stopForeground(true);
	}

	public void notifyNetwork(boolean enabled) {
		isWifiOn = enabled;
		checkStart();
	}
	
	public void notifyScreen(boolean isOn) {
		isScreenOff = !isOn;
		checkStart();
	}


}
